package com.tz.online.order.action;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tz.online.entity.Cart;
import com.tz.online.entity.OrderItem;
import com.tz.online.entity.Pageing;
import com.tz.online.entity.User;
import com.tz.online.order.service.IOrderService;
import com.tz.online.util.BeanFactory;

@WebServlet(name="BalanceOrderAction",urlPatterns="/permission/order/balance")
public class BalanceOrderAction extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8493847754635065607L;
	IOrderService os = (IOrderService)BeanFactory.getBean("orderService");
	
	@SuppressWarnings("all")
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//1.根据用户查询出四个地址
		
		//2,接收提交过来的商品ID
		
		//3.通过和购物车中的商品比较，得到要提交的商品明细
		
		//4.把购物车放到session中，地址分页对象和要提交的商品明细放到request中，转发到订单提交确认页面
		
		
		User user = (User) req.getSession().getAttribute("userInfo");
		Pageing as = os.queryAllAddressByUser(user,4,1);
		
		
		String ids = req.getParameter("ids");
		Cart cart = (Cart) req.getSession().getAttribute("cart");
		
		Set<OrderItem> items = cart.getItems();
		Set<OrderItem> selectItem = new HashSet<OrderItem>();
		
		String[] arr = ids.split(":");
		
		int count = 0;
		double allPrice = 0.0;
		for(OrderItem oi :items){
			for(String id:arr){
				if(oi.getBook().getBookId().toString().equals(id)){
					selectItem.add(oi);
					count += oi.getCount();
					allPrice += oi.getAllPrice();
				}	
			}
		}
		
		req.setAttribute("as", as);
		req.setAttribute("selectItem", selectItem);
		
		req.getRequestDispatcher("/jsp/order/confirm_order.jsp").forward(req, resp);
		
	}
}
